/**
 * 安全隐患清单
 */
 <template>
  <vp-main-page @refresh="loadDataList" :tableRef="tableRef">
		<template #search>
			<el-form ref="searchFormRef" :model="searchForm" @submit.prevent @keyup.enter="loadDataList" label-width="120px">
				<vp-search-row :show-more="showMore">
					<vp-search-col>
						<el-form-item prop="keyword">
							<template #label><vp-label zh="关键字" en="Keyword"/></template>
							<el-input v-model="searchForm.keyword" v-trim></el-input>
						</el-form-item>
					</vp-search-col>
					<vp-search-col>
						<el-form-item prop="fxrq">
							<template #label><vp-label zh="发现日期" en="Discovery Date"/></template>
							<el-date-picker
								v-model="searchForm.fxrq"
								type="daterange"
								start-placeholder="开始日期/Start Date"
								end-placeholder="结束日期/End Date"
								value-format="YYYY-MM-DD"
								unlink-panels
								range-separator="-"
								class="w100"
							>
							</el-date-picker>
						</el-form-item>
					</vp-search-col>
					<vp-search-col>
						<el-form-item prop="zdbsList">
							<template #label><vp-label zh="重大安全隐患" en="Major Hazard" /></template>
							<el-select v-model="searchForm.zdbsList" multiple collapse-tags>
								<el-option label="是 YES" :key="1" :value="1"></el-option>
								<el-option label="否 NO" :key="0" :value="0"></el-option>
							</el-select>
						</el-form-item>
					</vp-search-col>
					<vp-search-col>
						<el-form-item prop="zgbmmc">
							<template #label><vp-label zh="整改单位/部门" en="Unit/Dept" /></template>
							<el-input v-model="searchForm.zgbmmc" v-trim></el-input>
						</el-form-item>
					</vp-search-col>
					<vp-search-col>
						<el-form-item prop="yhlbList">
							<template #label><vp-label zh="隐患类别" en="Hazard Cat."/></template>
							<DictMulitSelect 
								:lxid='2009'
								:selectedId="searchForm.yhlbList"
								@dictMulitSelected='(data: any) => {searchForm.yhlbList = data}'
								class="w100"
							/>
						</el-form-item>
					</vp-search-col>
					<vp-search-col>
						<el-form-item prop="lyList">
							<template #label><vp-label zh="来源" en="Source"/></template>
							<DictMulitSelect 
								:lxid='2008'
								:selectedId='searchForm.lyList'
								@dictMulitSelected='(data: any) => {searchForm.lyList = data}'
								class="w100"
							/>
						</el-form-item>
					</vp-search-col>
					<vp-search-col>
						<el-form-item prop="gbbsList">
							<template #label><vp-label zh="是否关闭" en="Is Closed" /></template>
							<el-select v-model="searchForm.gbbsList" multiple collapse-tags>
								<el-option label="是 YES" :key="1" :value="1"></el-option>
								<el-option label="否 NO" :key="0" :value="0"></el-option>
							</el-select>
						</el-form-item>
					</vp-search-col>
					<vp-search-col>
						<el-form-item prop="ztList">
							<template #label><vp-label zh="状态" en="Status"/></template>
							<VpStatusMultSelect v-model="searchForm.ztList" />
						</el-form-item>
					</vp-search-col>
					<vp-search-col btn-col>
						<vp-btn-search :loading="loading" @click="loadDataList"/>
						<vp-btn-reset @click="handleSearchReset"/>
						<vp-btn-more @change="showMore = $event"/>
					</vp-search-col>
				</vp-search-row>
			</el-form>
		</template>
		<template #actions>
			<vp-btn-add v-if="jb == 20" v-auth="'qualitySafetyOrgHiddenTrouble.add'" @click="handleAdd"/>
			<vp-btn-add v-if="jb == 10" v-auth="'qualitySafetyDeptHiddenTrouble.add'" @click="handleAdd"/>
			<vp-btn-export v-if="type === 'list'" @click="handleExport"/>
		</template>
		<template #table>
			<vp-vxe-table
				:id="`${props.jb === 10 ? 'qualitySafetyDeptHiddenTroubleTable' : 'qualitySafetyOrgHiddenTroubleTable'}`"
				ref="tableRef" 
				:data="tableData" 
				@sort-change="handleSortChange" 
				row-key="id"
				v-loading="loading"
			>	
				<vxe-column type="seq" align="center" title="#" width="50" />
				<vxe-column sortable field="bh" title="隐患编号 Hazard No." show-overflow :min-width="jb == 10 ? 180 : 140">
					<template #header><vp-label zh="隐患编号" en="Hazard No."/></template>
				</vxe-column>
				<vxe-column sortable field="mc" title="安全隐患名称 Hazard Name" show-overflow min-width="250">
					<template #header><vp-label zh="安全隐患名称" en="Hazard Name"/></template>
					<template #default="scope">
						<span v-html="markTextColor(searchForm.keyword, scope.row.mc)"/>
					</template>
				</vxe-column>
				<vxe-column sortable field="zdbs" title="重大安全隐患 Major Hazard" show-overflow min-width="140">
					<template #header><vp-label zh="重大安全隐患" en="Major Hazard"/></template>
					<template #default="scope">
						{{scope.row.zdbs == 1 ? '是 YES' : scope.row.zdbs == 0 ? '否 NO' : ''}}
					</template>
				</vxe-column>
				<vxe-column sortable field="yhlb" title="隐患类别 Hazard Cat." show-overflow min-width="150">
					<template #header><vp-label zh="隐患类别" en="Hazard Cat."/></template>
				</vxe-column>	
				<vxe-column sortable field="yyfx" title="原因分析 Cause Analysis" show-overflow min-width="300">
					<template #header><vp-label zh="原因分析" en="Cause Analysis"/></template>
				</vxe-column>
				<vxe-column sortable field="gldfxkzcs" title="关联的风险控制措施 Related Risk Control Measures" show-overflow min-width="300">
					<template #header><vp-label zh="关联的风险控制措施" en="Related Risk Control Measures"/></template>
				</vxe-column>
				<vxe-column sortable field="gldhg" title="关联的后果 Assoc. Consequences" show-overflow min-width="200">
					<template #header><vp-label zh="关联的后果" en="Assoc. Consequences"/></template>
				</vxe-column>
				<vxe-column sortable field="ly" title="来源 Source" show-overflow min-width="120">
					<template #header><vp-label zh="来源" en="Source"/></template>
				</vxe-column>
				<vxe-column sortable field="fxrq" title="发现日期 Discovery Date" show-overflow min-width="140">
					<template #header><vp-label zh="发现日期" en="Discovery Date"/></template>
				</vxe-column>
				<vxe-column sortable field="zgbmmc" title="整改单位/部门 Unit/Department" show-overflow min-width="150">
					<template #header><vp-label zh="整改单位/部门" en="Unit/Department"/></template>
				</vxe-column>
				<vxe-column sortable field="zgrq" title="整改日期 Rect. Date" show-overflow min-width="140">
					<template #header><vp-label zh="整改日期" en="Rect. Date"/></template>
				</vxe-column>
				<vxe-column sortable field="zgcs" title="整改措施 Rect. Measure" show-overflow min-width="300">
					<template #header><vp-label zh="整改措施" en="Rect. Measure"/></template>
				</vxe-column>
				<vxe-column sortable field="zgzj" title="整改资金 Rect. Fund" show-overflow min-width="140">
					<template #header><vp-label zh="整改资金" en="Rect. Fund"/></template>
				</vxe-column>
				<vxe-column sortable field="yjya" title="应急预案 Emergency Plan" show-overflow min-width="150">
					<template #header><vp-label zh="应急预案" en="Emergency Plan"/></template>
				</vxe-column>
				<vxe-column sortable field="csyzr" title="措施验证人 Measures Verifier" show-overflow min-width="150">
					<template #header><vp-label zh="措施验证人" en="Measures Verifier"/></template>
				</vxe-column>
				<vxe-column sortable field="yzrq" title="验证日期 Verification Date" show-overflow min-width="145">
					<template #header><vp-label zh="验证日期" en="Verification Date"/></template>
				</vxe-column>
				<vxe-column sortable field="zlxgyzqk" title="治理效果验证情况 Verification Status" show-overflow min-width="300">
					<template #header><vp-label zh="治理效果验证情况" en="Verification Status"/></template>
				</vxe-column>
				<vxe-column sortable field="gbbs" title="是否关闭 Is Closed" show-overflow min-width="120">
					<template #header><vp-label zh="是否关闭" en="Is Closed"/></template>
					<template #default="scope">
						{{scope.row.gbbs == 1 ? '是 YES' : scope.row.gbbs == 0 ? '否 NO' : ''}}
					</template>
				</vxe-column>
				<vxe-column sortable field="gbrq" title="关闭日期 Closed Date" show-overflow min-width="120">
					<template #header><vp-label zh="关闭日期" en="Closed Date"/></template>
					<template #default="scope">
						{{scope.row.gbbs == 1 ? scope.row.gbrq : ''}}
					</template>
				</vxe-column>
				<vxe-column sortable field="zt" title="状态 Status" show-overflow min-width="110">
					<template #header><vp-label zh="状态" en="Status"/></template>
					<template #default="scope">
						{{ getStatusDesc(scope.row.zt) }}
					</template>
				</vxe-column>
				<vxe-column sortable field="lydj" title="来源单据 Source Doc." show-overflow min-width="200">
					<template #header><vp-label zh="来源单据" en="Source Doc."/></template>
					<template #default="scope">
						<el-link v-if="scope.row.glaqyhid" type="info" class="foc-link" :underline="false"  @click="handleViewGlyh(scope.row)">由部门转入:{{ scope.row.glaqyhbh}}</el-link>
						<el-link v-else-if="scope.row.glzgfkdid" type="info" class="foc-link" :underline="false"  @click="handleViewZgfkd(scope.row)">整改反馈单:{{ scope.row.zgfkdbh}}</el-link>
						<el-link v-else-if="scope.row.glzybgid" type="info" class="foc-link" :underline="false"  @click="handleViewZybg(scope.row)">{{scope.row.lylx === 20 ? '自愿报告' : '举报信息'}}:{{ scope.row.zybgbh}}</el-link>
						<span v-else>手工录入</span>
					</template>
				</vxe-column>
				<vxe-column field="operation" title="操作 Operations" fixed="right"  :min-width="jb == 10 ? 290 : 200">
					<template #header><vp-label-operations/></template>
					<template #default="scope">
						<el-space>
							<vp-btn-edit v-if="scope.row.zt == 10 && jb == 10" text v-auth="'qualitySafetyDeptHiddenTrouble.edit'" @click="handleEdit(scope.row)"/>
							<vp-btn-del v-if="scope.row.zt == 10 && jb == 10" text v-auth="'qualitySafetyDeptHiddenTrouble.delete'" @click="handleDelete(scope.row)"/>
							<vp-btn-discard v-if="(scope.row.zt === 88 || scope.row.zt === 89) && jb == 10" text v-auth="'qualitySafetyDeptHiddenTrouble.discard'" @click="handleCancle(scope.row)"/>
							<vp-btn-edit v-if="scope.row.zt == 10 && jb == 20" text v-auth="'qualitySafetyOrgHiddenTrouble.edit'" @click="handleEdit(scope.row)"/>
							<vp-btn-del v-if="scope.row.zt == 10 && jb == 20" text v-auth="'qualitySafetyOrgHiddenTrouble.delete'" @click="handleDelete(scope.row)"/>
							<vp-btn-discard v-if="(scope.row.zt === 88 || scope.row.zt === 89) && jb == 20" text v-auth="'qualitySafetyOrgHiddenTrouble.discard'" @click="handleCancle(scope.row)"/>
							<vp-btn type="primary" v-if="scope.row.zt == 89  && scope.row.existGl == 0 && scope.row.jb == 10" text v-auth="'qualitySafetyDeptHiddenTrouble.edit'" @click="handle2Company(scope.row)">
								<vp-label zh="纳入公司级清单" en="Included in Comp."/>
							</vp-btn>
							<vp-btn type="primary" v-if="scope.row.zt == 89 && jb == 10" text v-auth="'qualitySafetyDeptHiddenTrouble.edit'" @click="handleForceEdit(scope.row)">
								<vp-label zh="强制修改" en="Mandatory Rev."/>
							</vp-btn>
							<vp-btn type="primary" v-if="scope.row.zt == 89 && jb == 20" text v-auth="'qualitySafetyOrgHiddenTrouble.edit'" @click="handleForceEdit(scope.row)">
								<vp-label zh="强制修改" en="Mandatory Rev."/>
							</vp-btn>
							<!-- <vp-btn-print text @click="handlePrint(scope.row)"/> -->
							<vp-btn-view text @click="handleView(scope.row)"/>
						</el-space>
					</template>
				</vxe-column>
			</vp-vxe-table>
		</template>
		<template #pagination>
			<vp-pagination
				@change="loadDataList"
				:total="page.total" 
				v-model:currentPage="page.pageNum"
				v-model:pageSize="page.pageSize"
			>
			</vp-pagination>
		</template>
		<EditDeptHiddenTrouble ref="editDialogRef" @formSubmited="handleFormSubmited"></EditDeptHiddenTrouble>
	</vp-main-page>
</template>
<script setup lang="ts" name="hiddenTroubleCommon">
import { ref, reactive, onMounted } from 'vue';
import request from '/@/utils/request';
import { markTextColor } from '/@/utils/toolsValidate';
import EditDeptHiddenTrouble from "./component/EditDeptHiddenTrouble.vue";
import router from '/@/router';
import DictMulitSelect from "/@/components/vp/dict/VpDictSelectMulit.vue";
import { DEFAULT_PAGE } from '/@/utils/page';
import VpMessageBox from '/@/components/vp/VpMessageBox';
import commonFunction from '/@/utils/commonFunction';
import VpStatusMultSelect from "/@/components/vp/status/VpStatusMultSelect.vue";

const searchForm = reactive({
	keyword: "",
	ztList: [10,88,89],
	fxrq:[],
	zgbmmc: "",
	yhlbList: [],
	lyList: [],
	gbbsList: [],
	zdbsList: [],
	jb: 10, // 级别 10部门 20公司
	sygn: 41
});

const props = defineProps({
	jb: {
		type: Number,
	},
	sygn: {
		type: String,
	},
	type: {
		type: String,
	},
});

const page = reactive({
	...DEFAULT_PAGE,
})

const showMore = ref(false);
const loading = ref(false);
const tableData = ref([]);

// 加载列表数据
const loadDataList = () => {
	loading.value = true
	request.get('/safe/deptHiddenTrouble/list', {
		params: {
			...searchForm,
			...page,
		}
	}).then(res => {
		tableData.value = res?.data?.list || [];
		page.total = res?.data?.total || 0;
		page.pageNum = res?.data?.pageNum || 1;
	}).finally(()=>{
		loading.value = false;
	})
}

const searchFormRef = ref();
const tableRef = ref();

// 重置搜索条件
const handleSearchReset = ()=>{
	searchFormRef.value.resetFields();
}

// 列表字段排序
const handleSortChange = (column: any)=>{
	page.sortColumn = column.field;
	page.sortOrder = column.order;
	loadDataList()
}

onMounted(() => {
	if(props.jb){
		searchForm.jb = props.jb;
	}
	if(props.sygn){
		searchForm.sygn = props.sygn;
	}
	loadDataList();
});

const editDialogRef = ref();

const handleAdd = ()=>{
	editDialogRef.value.handleOpen('add','', props.jb || 10);
};
const handleView = (row:any)=>{
	router.push({
		name: 'viewHiddenTrouble',
		params: {
			id: row.id,
		},
	});
}
const handleViewGlyh = (row:any)=>{
	router.push({
		name: 'viewHiddenTrouble',
		params: {
			id: row.glaqyhid,
		},
	});
}

const handleViewZgfkd = (row:any) =>{
	router.push({
	name: 'viewRectification',
	params: {
		id: row.glzgfkdid,
	},
	});
}  

const handleViewZybg = (row: any) =>{
if(row.lylx == 20){
router.push({
	name: 'viewVoluntaryOrgHandle',
	params: {
	id: row.glzybgmxid,
	},
});
}else {
router.push({
	name: 'viewReportOrgHandle',
	params: {
	id: row.glzybgmxid,
	},
});
}
}


const handleEdit = (row: any)=>{
	editDialogRef.value.handleOpen('edit', row.id, row.jb);
}

const handleForceEdit = (row: any)=>{
	editDialogRef.value.handleOpen('forceEdit', row.id, row.jb);
}

// 删除
const handleDelete = (row: any)=>{
	VpMessageBox.confirmDelete().then(()=>{
		loading.value = true;
		request.delete(`/safe/deptHiddenTrouble/${row.id}/${row.sjbb}`).then(res => {
			loadDataList();
		}).catch(() => {}).finally(() => {
			loading.value = false;
		})
	});
}

// 终止
const handleCancle = (row: any)=>{
	VpMessageBox.confirmOperation().then(()=>{
		loading.value = true;
		request.post(`/safe/deptHiddenTrouble/cancle/${row.id}/${row.sjbb}`).then(res => {
			loadDataList();
		}).catch(() => {}).finally(() => {
			loading.value = false;
		})
	});
}

// 纳入公司级清单
const handle2Company = (row: any)=>{

	VpMessageBox.confirmOperation().then(()=>{
		loading.value = true;
		request.post(`/safe/deptHiddenTrouble/company/${row.id}/${row.sjbb}`).then(res => {
			loadDataList();
		}).catch(() => {}).finally(() => {
			loading.value = false;
		})
	});
}

const handleFormSubmited=()=>{
	loadDataList();
}

const {exportFile, printFile, getStatusDesc} = commonFunction();

const handleExport = () =>{
	if(!props.jb){
		return;
	}
	exportFile({
		url: `/safe/deptHiddenTrouble/export`,
		params: {
			...searchForm,
		},
		fileName: props.jb == 10 ? '部门级安全隐患清单.xlsx' : props.jb == 20 ? '公司级安全隐患清单.xlsx' : "安全隐患清单",
	});
}
const handlePrint = (row: any) =>{
	printFile({
		url: `/safe/deptHiddenTrouble/print/${row.id}`
	});
}

</script>
<style lang="scss" scoped>
</style>
